package com.ruoyi.common;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ruoyi.common.core.domain.entity.SysDictData;
import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Modifier;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Utils {
    private static GsonBuilder gsonBuilder = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss");
    private final static Gson gson = gsonBuilder.disableHtmlEscaping()
            .excludeFieldsWithModifiers(Modifier.PROTECTED, Modifier.TRANSIENT, Modifier.STATIC).create();

    public static String toJson(Object object) {
        return gson.toJson(object);
    }

    public static final String DATE_HOUR_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String DATE_FORMAT_NONE_SPLIT = "yyyyMMdd";

    public static HashMap<String, SysDictData> code2Map(List<SysDictData> dictDatas) {
        HashMap<String, SysDictData> dataHashMap = new HashMap<>();
        if (dictDatas != null) {
            for (int i = 0; i < dictDatas.size(); i++) {
                SysDictData tmp = dictDatas.get(i);
                dataHashMap.put(tmp.getDictValue(), tmp);
            }
        }
        return dataHashMap;
    }

    public static Long getDayBetween2Day(Date before) {
        if (before == null)
            return 0L;
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        try {
            String nowStr = sdf.format(new Date());
            String beforeStr = sdf.format(before);
            long now = sdf.parse(nowStr).getTime();
            long beforeTime = sdf.parse(beforeStr).getTime();
            long days = (now - beforeTime) / (24 * 60 * 60 * 1000);
            return days;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0L;
    }
//
//    public List<String> authors (String ) {
//        List<String> authorArry = new ArrayList<>();
//        if (authors != null) {
//            for (int i = 0; i < authors.length; i++) {
//                String auth = authors[i];
//                if (auth.startsWith("ROLE_"))
//                    authorArry.add(auth.substring("ROLE_".length()));
//            }
//        }
//        return
//    }

    public static Date getCurrentDate() {
        return new Date();
    }

    public static ArrayList<String> getDifference(String[] array1, String[] array2) {
        // 将数组转换为 HashSet
        HashSet<String> set1 = new HashSet<>(Arrays.asList(array1));
        HashSet<String> set2 = new HashSet<>(Arrays.asList(array2));

        // 创建一个新的 ArrayList 来存储差集
        ArrayList<String> difference = new ArrayList<>(set2);

        // 移除所有 array2 中的元素
        difference.removeAll(set1);

        return difference;
    }

    public static Boolean hasTeacherRole(Set<String> auth) {
        if (auth == null)
            return false;
        return auth.contains(Constant.ROLE_KEY_TEACHER);
    }

    public static Boolean hasAdminRole(Set<String> auth) {
        if (auth == null)
            return false;
        return auth.contains(Constant.ROLE_KEY_ADMIN);
    }

    public static String asciiToLetter(int num) {
        int code = num + 65;
        char ncode = (char) code;
        return ncode + "";
    }

    /**
     * 1-7
     **/
    public static int dayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        //1代表周日，2代表周一
        if (dayOfWeek == 1) {
            dayOfWeek = 7;
        } else {
            dayOfWeek = dayOfWeek - 1;
        }
        return dayOfWeek;
    }

    public static String UUIDGenerat(String str) {
        if (str == null)
            str = "";
        return UUIDGenerat().replace("-", str);
    }

    public static String UUIDGenerat() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString();
    }

    public static final String DEFAULT_REPLACE_CHAR = "*";

    /**
     * 保留前3
     **/
    public static Integer tel_front_count = 3;
    /**
     * 保留后3
     **/
    public static Integer tel_end_count = 3;

    /**
     * 保留前3
     **/
    public static Integer card_front_count = 3;
    /**
     * 保留后3
     **/
    public static Integer card_end_count = 3;

//		class Name{
//			//默认 王*  , 王*二  , 王*****二
//		}

    public static String SensitiveWordsReplace(Set<String> set) {
        return SensitiveWordsReplace(set, DEFAULT_REPLACE_CHAR);
    }

    public static String SensitiveWordsReplace(Set<String> set, String char_) {
        StringBuilder stringBuilder = new StringBuilder();
        for (String key : set) {
            if (StringUtils.hasLength(key)) {
                int size = key.length();
                switch (size) {
                    case 1:
                        //直接 *
                        stringBuilder.append(char_);
                        break;
                    case 2:
                        //末尾替换 a*
                        stringBuilder.append(key.substring(0, 1) + char_);
                        break;
                    default:
                        //除前后不替换 a****b
                        String first = key.substring(0, 1);
                        String end = key.substring(key.length() - 1);
                        String charResult = getChar(key.length() - 2, char_);
                        stringBuilder.append(String.format("%s%s%s", first, charResult, end));
                        break;
                }
            }
            stringBuilder.append(";");
        }
        return stringBuilder.toString();
    }

    public static String SensitiveWordsReplace(List<String> set, String char_) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < set.size(); i++) {
            String key = set.get(i);
            if (StringUtils.hasLength(key)) {
                int size = key.length();
                switch (size) {
                    case 1:
                        //直接 *
                        stringBuilder.append(char_);
                        break;
                    case 2:
                        //末尾替换 a*
                        stringBuilder.append(key.substring(0, 1) + char_);
                        break;
                    default:
                        //除前后不替换 a****b
                        String first = key.substring(0, 1);
                        String end = key.substring(key.length() - 1);
                        String charResult = getChar(key.length() - 2, char_);
                        stringBuilder.append(String.format("%s%s%s", first, charResult, end));
                        break;
                }
            }
            stringBuilder.append(";");
        }
        return stringBuilder.toString();
    }

    public static String WordsReplace(String key) {
        return WordsReplace(key, DEFAULT_REPLACE_CHAR);
    }

    public static String WordsReplace(String key, String char_) {
        StringBuilder stringBuilder = new StringBuilder();
        if (StringUtils.hasLength(key)) {
            int size = key.length();
            switch (size) {
                case 1:
                    //直接 *
                    //stringBuilder.append(char_);
                    break;
                case 2:
                    //末尾替换 a*
                    stringBuilder.append(key.substring(0, 1) + char_);
                    break;
                case 3:
                    String k1 = key.substring(0, 1);
                    String k2 = key.substring(2);
                    stringBuilder.append(k1 + char_ + k2);
                    break;
                case 11:
                    String ktel1 = key.substring(0, tel_front_count);
                    String ktel2 = key.substring(size - tel_end_count);
                    String tel_char = getChar(size - (tel_end_count + tel_front_count), char_);
                    stringBuilder.append(ktel1 + tel_char + ktel2);
                    break;
                case 18:
                    String card1 = key.substring(0, card_front_count);
                    String card2 = key.substring(size - card_end_count);
                    String card_char = getChar(size - (card_end_count + card_front_count), char_);
                    stringBuilder.append(card1 + card_char + card2);
                    break;
                default:
                    //除前后不替换 a****b
                    String first = key.substring(0, 2);
                    String end = key.substring(key.length() - 1);
                    String charResult = getChar(key.length() - 3, char_);
                    stringBuilder.append(String.format("%s%s%s", first, charResult, end));
                    break;
            }
        }

        return stringBuilder.toString();
    }

    public static String getChar(int count, String char_) {
        StringBuilder stringBuilder = new StringBuilder();
        if (count > 10)
            count = 10;
        for (int i = 0; i < count; i++) {
            stringBuilder.append(char_);
        }
        return stringBuilder.toString();
    }

    public static String getSHA256(String str) {
        MessageDigest messageDigest;
        String encodeStr = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(str.getBytes("UTF-8"));
            encodeStr = AES128Util.parseByte2HexStr(messageDigest.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (StringUtils.hasLength(encodeStr))
            encodeStr = encodeStr.toLowerCase(Locale.ROOT);
        return encodeStr;

    }

    public static String timeStr(String format, Date date) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            return simpleDateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String timeStr(String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(new Date());
    }

    /**
     * yyyy-MM-dd HH:mm:ss to yyyy-MM-dd
     **/
    public static String splitDay(String date) {
        if (!StringUtils.hasLength(date))
            return null;
        return date.split(" ")[0];

    }

    /**
     * yyyy-MM-dd HH:mm:ss
     **/
    public static String timeStr() {
        return timeStr(DATE_HOUR_FORMAT);
    }

    /**
     * yyyy-MM-dd
     **/
    public static String dayStr() {
        return timeStr(DATE_FORMAT);
    }

    /**
     * yyyy-MM-dd
     **/
    public static String timeStr(Date date) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT);
            return simpleDateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

//    public static void main(String[] args) {
//       System.out.println( asciiToLetter(0));
//        System.out.println( asciiToLetter(1));
//        System.out.println( asciiToLetter(2));
//    }
}
